package com.example.graduation.controller;

import com.example.graduation.entity.CompanyJob;
import com.example.graduation.service.CompanyJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company-jobs")
public class CompanyJobController {
    @Autowired
    private CompanyJobService companyJobService;

    @GetMapping
    public ResponseEntity<List<CompanyJob>> getAllJobs() {
        List<CompanyJob> jobs = companyJobService.findAll();
        return ResponseEntity.ok(jobs);
    }

    @GetMapping("/{id}")
    public ResponseEntity<CompanyJob> getJobById(@PathVariable Integer id) {
        CompanyJob job = companyJobService.findById(id);
        if (job != null) {
            return ResponseEntity.ok(job);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/company/{email}")
    public ResponseEntity<List<CompanyJob>> getJobsByCompanyEmail(@PathVariable String email) {
        List<CompanyJob> jobs = companyJobService.findByCompanyEmail(email);
        return ResponseEntity.ok(jobs);
    }

    @PostMapping
    public ResponseEntity<CompanyJob> createJob(@RequestBody CompanyJob companyJob) {
        int result = companyJobService.insert(companyJob);
        if (result > 0) {
            return ResponseEntity.ok(companyJob);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<CompanyJob> updateJob(@PathVariable Integer id, @RequestBody CompanyJob companyJob) {
        companyJob.setId(id);
        int result = companyJobService.update(companyJob);
        if (result > 0) {
            return ResponseEntity.ok(companyJob);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteJob(@PathVariable Integer id) {
        int result = companyJobService.deleteById(id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 